﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Castle.ActiveRecord;
using Light.Utility;
using System.ComponentModel;

namespace Light.Framework.Model
{
    /// <summary>
    /// 机构
    /// </summary>
    /// <remarks></remarks>
    [Traceable]
    [Serializable]
    [ActiveRecord]
    [DisplayName("客户机构")]
    public class Organization : Entity
    {
        /// <summary>
        /// 名称/简称
        /// </summary>
        [Property("FName")]
        [Searchable("简称")]
        [ImportField("简称*")]
        public string Name { get; set; }

        /// <summary>
        /// 全称
        /// </summary>
        [Property]
        [Searchable("全称")]
        [ImportField("全称")]
        public string FullName { get; set; }
        /// <summary>
        /// 注册地址
        /// </summary>
        [Property]
        [JsonSerialize("OrganizationAddress")]
        [Searchable("地址")]
        [ImportField("地址*")]
        public string Address { get; set; }
        /// <summary>
        /// 法人代表
        /// </summary>
        [Property]
        [Searchable]
        [ImportField("联系人*")]
        public string Delegate { get; set; }

        /// <summary>
        /// 电话
        /// </summary>
        [Property]
        [Searchable("电话")]
        [ImportField("电话*")]
        public string Phone { get; set; }


        [Property]
        public bool IsSystem { get; set; }

        /// <summary>
        /// 是否包含下级机构
        /// </summary>
        [Property]
        public bool HasChildren { get; set; }


        /// <summary>
        /// 最近一次充值时间
        /// </summary>
        [Property]
        [ImportField("最近充值")]
        public DateTime TimePaid { get; set; }

        /// <summary>
        /// 截止时间
        /// </summary>
        [Property]
        [ImportField("截止时间")]
        public DateTime TimeExpire { get; set; }

        /// <summary>
        ///有效期至
        /// </summary>
        public string TimeExpireDesc
        {
            get
            {
                string time = string.Empty;

                if (IsFree)
                    time = "免费使用版";
                else if (TimeExpire < DateTime.Now.AddYears(50))
                {
                    if (TimePaid != default(DateTime))
                        time = string.Format("最近充值日期：{0} 有效期至：{1}", TimePaid.ToString("yyyy-MM-dd"), TimeExpire.ToString("yyyy-MM-dd"));
                    else
                        time = string.Format("试期版本，有效期至：{0}", TimeExpire.ToString("yyyy-MM-dd"));
                }
                else
                    time = "永久使用版";

                return time;
            }
        }

        /// <summary>
        /// 版本说明
        /// </summary>
        public string VersionDesc
        {
            get
            {
                string time = string.Empty;

                if (!IsFree && TimeExpire < DateTime.Now.AddYears(50))
                    time = "系统提示：";
                else
                    time = "当前版本：";

                return time;
            }
        }

        /// <summary>
        /// 最近一次充值金额
        /// </summary>
        [Property]
        [ImportField("最近充值金额")]
        public decimal Amount { get; set; }

        /// <summary>
        /// 短信剩余条数
        /// </summary>
        [Property]
        [ImportField("短信剩余")]
        public int MessageBalance { get; set; }


        /// <summary>
        /// 省份
        /// </summary>
        [Property]
        public string Province { get; set; }

        /// <summary>
        /// 城市 
        /// </summary>
        [Property]
        public string City { get; set; }

        /// <summary>
        /// 地区
        /// </summary>
        [Property]
        public string District { get; set; }

        /// <summary>
        /// 是否为免费使用
        /// </summary>
        [Property]
        public bool IsFree { get; set; }

        public override string ToString()
        {
            return Name;
        }

        public override bool Equals(object obj)
        {
            bool equal = false;
            if (obj != null && obj is Organization && !this.IsNew)
            {
                equal = ID == ((Organization)obj).ID;
            }
            else
            {
                equal = base.Equals(obj);
            }
            return equal;
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

    }
}
